package com.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.os.storage.OnObbStateChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.objects.ContactCheck;
import com.objects.VaultFile;
import com.senditcertified.mail.R;
import com.views.ACheckBox;

public class VaultFilesCheckAdapter extends BaseAdapter implements
		OnClickListener {
	public static final int MODE_CHECK_ALL = 0;
	public static final int MODE_UNCHECK_ALL = 1;
	public static final int MODE_NORMAL = 3;
	private Context mContext;
	private ArrayList<VaultFile> lst;
	private LayoutInflater inflater = null;
	private int checkMode = MODE_NORMAL;
	private CheckBox mCheckAll;

	public VaultFilesCheckAdapter(Context mContext, CheckBox checkAll,
			ArrayList<VaultFile> lst) {
		this.mContext = mContext;
		this.lst = lst;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mCheckAll = checkAll;

	}

	public String getVaultChoosed() {
		String returnValue = "";
		for (int i = 0; i < getCount(); i++) {
			VaultFile check = getItem(i);
			if (check.isSelected()) {
				returnValue += (check.getName() + ";");
			}
		}
		return returnValue;
	}

	public ArrayList<VaultFile> getVaultChoosedList() {
		ArrayList<VaultFile> returnLst = new ArrayList<VaultFile>();
		for (int i = 0; i < getCount(); i++) {
			VaultFile check = getItem(i);
			if (check.isSelected()) {
				returnLst.add(check);
			}
		}
		return returnLst;
	}

	public void removeSelected() {
		int i = 0;
		while (i < getCount()) {
			VaultFile check = getItem(i);
			if (check.isSelected()) {
				lst.remove(i);
			} else {
				i++;
			}
		}
		notifyDataSetChanged();
	}

	public int getCount() {
		if (lst == null)
			return 0;
		else
			return lst.size();
	}

	public void setCheckMode(int checkMode) {
		this.checkMode = checkMode;
	}

	public VaultFile getItem(int position) {
		return lst.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		VaultFile item = getItem(position);
		if (convertView == null) {
			v = inflater.inflate(R.layout.item_vault_file, null);
		}
		TextView tvName = (TextView) v.findViewById(R.id.vault_file_name_tv);
		TextView tvSize = (TextView) v.findViewById(R.id.vault_file_size_tv);
		TextView tvDate = (TextView) v.findViewById(R.id.vault_file_date_tv);
		CheckBox mCb = (CheckBox) v.findViewById(R.id.vault_select_cb);

		tvName.setText(item.getName());
		tvSize.setText(item.getSize());
		tvDate.setText(item.getDate());

		if (checkMode == MODE_CHECK_ALL) {
			item.setSelected(true);
		} else if (checkMode == MODE_UNCHECK_ALL) {
			item.setSelected(false);
		}
		mCb.setChecked(item.isSelected());
		mCb.setTag(position);
		mCb.setOnClickListener(this);
		tvName.setSelected(true);
		tvSize.setSelected(true);
		tvDate.setSelected(true);

		return v;
	}

	public ArrayList<VaultFile> getLst() {
		return lst;
	}

	// process checkall for checkbox
	public void setCheckAll() {
		boolean check = true;
		for (int i = 0; i < getCount(); i++) {
			if (!getItem(i).isSelected()) {
				check = false;
				break;
			}
		}
		mCheckAll.setChecked(check);
	}

	@Override
	public void onClick(View v) {
		getItem(Integer.parseInt(v.getTag().toString())).setSelected(
				((CheckBox) v).isChecked());
		setCheckAll();
	}

}